public class LeetCode {

//    力扣 209 长度最小的子数组
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int sum = 0;
        int len = Integer.MAX_VALUE;

        for (int left = 0, right = 0; right < n; right++) {
            sum += nums[right];
             while (sum >= target) { // 判断，减小窗口，寻找最小窗口
                len = Math.min(len, right - left + 1);
                sum -= nums[left++];
            }
            // 跳出 while 循环，for 循环继续添加 nums[right] 的值，然后继续判断，寻找最小窗口
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }

//    无重复字符的最长子串
    public int lengthOfLongestSubstring(String ss) {
        char[] s = ss.toCharArray();
        int[] hash = new int[128]; // ⽤数组模拟哈希表
        int left = 0, right = 0, n = ss.length();
        int ret = 0;
        while (right < n) {
            hash[s[right]]++; // 进⼊窗⼝
            while (hash[s[right]] > 1) // 判断
                hash[s[left++]]--; // 出窗⼝
            ret = Math.max(ret, right - left + 1); // 更新结果
            right++; // 让下⼀个字符进⼊窗⼝
        }
        return ret;
    }

//    力扣 1004
    public int longestOnes(int[] nums, int k) {
        int ret = 0;
        for(int left = 0,right = 0,zero = 0; right < nums.length;right++){
            if (nums[right] == 0){
                zero++;
            }
            while(zero > k){
                if (nums[left++] == 0){
                    zero--;
                }
                ret = Math.max(ret,right-left+1);
            }
        }
        return ret;
    }

}
